<?php

namespace app\home\model;
use think\Model;

class Prop extends Model
{
    protected $pk = 'ID';

    protected $table = 'user_props_info';
    
    protected $connection = 'db_config2';

    /**
     * 竞技礼包 7067 7068
     * 处理特殊的道具10528 
     * 当日首次购买 道具个数直接覆盖 不累加 道具过期时间 23:59:59
     * 
     * @return void
     */
    public function processComptetion($UserID,$goods_id,$extra){
        $prop_id = $extra[0];
        $where['UserID'] = $UserID;
        $where['PropID'] = $prop_id;

        $end_date = date('Y-m-d 23:59:59');
        $count = $extra[1];
        // 10528道具 累加这个道具数量
        if($res = $this->isHas($UserID,$prop_id)){
            // 通过过期时间判断是否今天第一次购买这个道具
            if($res['ExpireTime'] != $end_date){
                self::where('PropID',$prop_id)->where('UserID', $UserID)
                    ->update(['Counts'=>$count,'ExpireTime'=>$end_date]);
            }else{
                self::where('PropID',$prop_id)->where('UserID', $UserID)
                    ->update(['Counts'=>$count+$res['Counts'],'ExpireTime'=>$end_date]);
            }
        }else{
            $data['UserID'] = $UserID;
            $data['PropID'] = $prop_id;
            $data['Counts'] = $count;
            $data['Type'] = $goods_id;
            $data['InserTime'] = date('Y-m-d H:i:s');
            $data['ExpireTime'] = $end_date;

            self::create($data);
        }
       
        return true;
    }

    /**
     * 获取某个玩家是否拥有某个道具
     * 返回个数 或者 false
     * @param [int] $UserID
     * @param [int] $PropID
     * @return boolean
     */
    public function isHas($UserID,$PropID){
        $where['UserID'] = $UserID;
        $where['PropID'] = $PropID;
  
        $row = self::where($where)->find();
        if($row){
            return $row;
        }

        return false;
    }

}